# Maintainer: Alexey Pavlov <alexpux@gmail.com>

_realname=cgal
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=5.0.2
pkgrel=1
pkgdesc="Computational Geometry Algorithms Library (mingw-w64)"
arch=('any')
url="https://www.cgal.org/"
license=("LGPL, GPL")
makedepends=("${MINGW_PACKAGE_PREFIX}-gcc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-eigen3"
             "${MINGW_PACKAGE_PREFIX}-pkg-config"
             "${MINGW_PACKAGE_PREFIX}-qt5")
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-boost"
         "${MINGW_PACKAGE_PREFIX}-gmp"
         "${MINGW_PACKAGE_PREFIX}-mpfr")
optdepends=("${MINGW_PACKAGE_PREFIX}-eigen3: for some packages, see the CGAL manual"
            "${MINGW_PACKAGE_PREFIX}-qt5: for CGAL_Qt5"
           )
options=('strip' 'staticlibs')
source=(https://github.com/CGAL/cgal/releases/download/releases/CGAL-${pkgver}/CGAL-${pkgver}.tar.xz
        msys2-relocate-cmake-config.patch)
sha256sums=('bb3594ba390735404f0972ece301f369b1ff12646ad25e48056b4d49c976e1fa'
            'e4dc11a835f94d6b6b70e548a321dc6b1cf793e9d7417bb6cdb61c9b49e64d86')

prepare() {
  cd "${srcdir}"/CGAL-${pkgver}
  patch -p1 -i "${srcdir}"/msys2-relocate-cmake-config.patch
}

build() {
  [[ -d ${srcdir}/build-${MINGW_CHOST} ]] && rm -rf ${srcdir}/build-${MINGW_CHOST}
  mkdir ${srcdir}/build-${MINGW_CHOST}
  cd ${srcdir}/build-${MINGW_CHOST}

  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
  ${MINGW_PREFIX}/bin/cmake \
      -G"MSYS Makefiles" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DCMAKE_BUILD_TYPE=Release \
      ../CGAL-${pkgver}

  make
}

package() {
  cd "${srcdir}/build-${MINGW_CHOST}"
  make DESTDIR=${pkgdir} install
  local MINGW_PREFIX_WIN=$(cygpath -am ${MINGW_PREFIX})
  sed -s "s|${MINGW_PREFIX_WIN}|\${RELOCATED_INSTALL_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/cmake/CGAL/CGALConfig.cmake
  sed -s "s|${MINGW_PREFIX}|\${RELOCATED_INSTALL_PREFIX}|g" -i "${pkgdir}"${MINGW_PREFIX}/lib/cmake/CGAL/CGALConfig.cmake
}
